package cn.wd.web.controller;

import cn.wd.domain.Book;
import cn.wd.qo.QueryObject;
import cn.wd.service.IBookService;
import cn.wd.vo.JsonResult;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/book")
public class BookController {

    private final IBookService bookService;

    public BookController(IBookService bookService) {
        this.bookService = bookService;
    }

    @RequestMapping("/list")
    public JsonResult<PageInfo<Book>> list(QueryObject qo) {
        return JsonResult.success(bookService.selectForList(qo));
    }

    @RequestMapping("/saveOrUpdate")
    public JsonResult<?> saveOrUpdate(@RequestBody Book book) {
        if (book.getId() != null) {
            bookService.update(book);
        } else {
            bookService.insert(book);
        }
        return JsonResult.success(null);
    }

    @RequestMapping("/delete/{id}")
    public JsonResult<List<Book>> delete(@PathVariable Long id) {
        bookService.delete(id);
        return JsonResult.success(null);
    }

    
}
